<!-- 健康雷达图 -->

<template>
  <div class="health-radar">
    <div ref="radarChart" class="radar-chart"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'HealthRadar',
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
      // 监听窗口大小变化，自适应调整图表
      window.addEventListener('resize', this.handleResize)
    })
  },
  beforeDestroy() {
    // 组件销毁前清理图表实例和事件监听
    window.removeEventListener('resize', this.handleResize)
    if (this.chart) {
      this.chart.dispose()
      this.chart = null
    }
  },
  methods: {
    initChart() {
      // 初始化图表实例
      this.chart = echarts.init(this.$refs.radarChart)
      
      // 雷达图配置选项
      const option = {
       
        tooltip: {
          trigger: 'item',
          formatter: function(params) {
            return `${params.name}: ${params.value}`
          }
        },
        radar: {
          indicator: [
            { name: '电气系统', max: 100 },
            { name: '温度系统', max: 100 },
            { name: '维修历史', max: 100 },
            { name: '告警频率', max: 100 }
          ],
          center: ['50%', '50%'],
          radius: '60%',
          name: {
            textStyle: {
              color: '#666',
              fontSize: 12
            }
          },
          splitArea: {
            areaStyle: {
              color: 'rgba(250, 250, 250, 0.3)'
            }
          },
          splitLine: {
            lineStyle: {
              color: '#ddd'
            }
          },
          axisLine: {
            lineStyle: {
              color: '#ddd'
            }
          }
        },
        series: [
          {
            name: '健康评分',
            type: 'radar',
            data: [
              {
                value: [85, 75, 80, 70],
                name: '当前评分',
                areaStyle: {
                  color: 'rgba(64, 158, 255, 0.3)'
                },
                lineStyle: {
                  color: '#409EFF',
                  width: 2
                },
                itemStyle: {
                  color: '#409EFF'
                }
              },
              {
                value: [90, 85, 90, 75],
                name: '标准评分',
                areaStyle: {
                  color: 'rgba(64, 158, 255, 0.2)'
                },
                lineStyle: {
                  color: '#409EFF',
                  width: 2,
                  type: 'dashed'
                },
                itemStyle: {
                  color: '#409EFF'
                }
              }
            ]
          }
        ]
      }
      
      // 设置配置项并渲染图表
      this.chart.setOption(option)
    },
    handleResize() {
      // 窗口大小改变时重新调整图表大小
      if (this.chart) {
        this.chart.resize()
      }
    }
  }
}
</script>

<style scoped>
.health-radar {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.radar-chart {
  width: 100%;
  height: 100%;
  min-height: 0;
}
</style>